package edu.neu.swc.cloud.service.impl;

import edu.neu.swc.cloud.dao.OrderDao;
import edu.neu.swc.cloud.dao.PlanDao;
import edu.neu.swc.cloud.domain.FacOrder;
import edu.neu.swc.cloud.domain.Order;
import edu.neu.swc.cloud.service.OrderService;
import edu.neu.swc.cloud.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("orderService")
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderDao dao;

    @Autowired
    PlanDao planDao;

    @Override
    public void hitOrder(int fac_id, int ord_id) {
        FacOrder facOrder = new FacOrder();
        facOrder.setFo_fac_id(fac_id);
        facOrder.setFo_ord_id(ord_id);
        dao.addFacOrd(facOrder);
        dao.setOtherDisable(ord_id);
        dao.setHitById(ord_id);
    }

    @Override
    public void addOrder(Order order) {
        order.setOrd_code(UuidUtil.getOrdCode());
        dao.addOrder(order);
    }

    @Override
    public void deleteOrderById(int ord_id) {
        dao.deleteOrderById(ord_id);
    }

    @Override
    public void updateOrder(Order order) {
        dao.updateOrder(order);
    }

    @Override
    public List<Order> findOrderByPage(int start, int rows, Map<String, String[]> condition) {
        return null;
    }

    @Override
    public int findTotalCount(Map<String, String[]> condition) {
        return 0;
    }

    @Override
    public void releaseById(int ord_id) {
        dao.releaseById(ord_id);
    }

    @Override
    public void endById(int ord_id) {
        dao.endById(ord_id);
    }

    @Override
    public Order findOrdById(int ord_id) {
        return dao.findOrdById(ord_id);
    }

    @Override
    public List<Order> findAll() {
        return dao.findAll();
    }

    @Override
    public List<Order> findOrderByCon(Order condition) {
        return dao.findOrdByCon(condition);
    }

    @Override
    public List<Order> findOrdByFac(int my_fac_id) {
        return dao.findOrdByFacId(my_fac_id);
    }

    @Override
    public List<Order> findOrdAva() {
        return dao.findOrdAva();
    }

    @Override
    public void endPlanById(int ord_id) {
        //从计划表中移除该订单
        planDao.removePlanByOrdId(ord_id);
        //修改订单状态为4
        dao.endPlanById(ord_id);
    }

    @Override
    public void transportById(int ord_id) {
        dao.transportById(ord_id);
    }

    @Override
    public List<Order> findOrdByFacAndCon(int my_fac_id, Order order) {
        List<Order> fac_order = dao.findOrdByFacId(my_fac_id);
        List<Order> con_order = dao.findOrdByCon(order);
        List<Order> result = new ArrayList<>();
        for(Order o : fac_order){
            for(Order o1 : con_order){
                if(o.getOrd_id() == o1.getOrd_id()){
                    result.add(o);
                }
            }
        }
        return result;
    }

    @Override
    public List<Order> findOrdAvaByCon(Order order) {
        return dao.findOrdAvaByCon(order);
    }

    @Override
    public int getOrderCounts(String ord_code) {
        return dao.getOrderCounts(ord_code);
    }

    @Override
    public List<Order> findOrderByQuery(String ord_code, int pageStart, int pageSize) {
        return dao.findOrdByQuery(ord_code,pageStart,pageSize);
    }

    @Override
    public int getMyOrderCounts(String ord_code, int fac) {
        return dao.getMyOrderCounts(ord_code,fac);
    }

    @Override
    public List<Order> findMyOrderByQuery(String ord_code, int fac, int pageStart, int pageSize) {
        return dao.findMyOrdByQuery(ord_code,fac,pageStart,pageSize);
    }

    @Override
    public int getReleasedOrderCounts(String ord_code) {
        return dao.getReleasedOrderCounts(ord_code);
    }

    @Override
    public List<Order> findReleasedOrderByQuery(String ord_code, int pageStart, int pageSize) {
        return dao.findReleasedOrderByQuery(ord_code,pageStart,pageSize);
    }
}
